package pl.nith.battleships.models;

import pl.nith.battleships.core.Model;
import pl.nith.battleships.helpers.BoardHelper;
import pl.nith.battleships.helpers.TileHelper.TileType;
import pl.nith.battleships.managers.GameStateManager.GameState;
import pl.nith.battleships.managers.GameStateManager.Turn;
import pl.nith.battleships.managers.Manager;

public class Board extends Model
{	
	private Tile[][] playerBoard;
	private Tile[][] hitBoard;
	
	public Tile getTile(int row, int col, boolean getHitBoard)
	{
		if (getHitBoard)
			return hitBoard[row][col];
		else
			return playerBoard[row][col];
	}
	
	public void setTile(int row, int col, TileType tileType, boolean setHitBoard)
	{
		if (Manager.getGMS().currentGameState == GameState.SETTING_SHIPS && !setHitBoard)
			if (row >= 0 && row < BoardHelper.ROWS && col >= 0 && col < BoardHelper.COLS)
				playerBoard[row][col].changeType(tileType);
		
		if (Manager.getGMS().turn == Turn.MY_TURN)
		{
			if (row >= 0 && row < BoardHelper.ROWS && col >= 0 && col < BoardHelper.COLS)
			{
				if (setHitBoard)
					hitBoard[row][col].changeType(tileType);
				else
					playerBoard[row][col].changeType(tileType);
			}
		}
		
		if (Manager.getGMS().turn == Turn.ENEMY_TURN)
		{
			if (row >= 0 && row < BoardHelper.ROWS && col >= 0 && col < BoardHelper.COLS)
//				hitBoard[row][column].changeType(tileType);
				playerBoard[row][col].changeType(tileType);
		}
	}
	
	public Board()
	{
		// Create a new 2D array of board tiles
		playerBoard = new Tile[BoardHelper.ROWS][BoardHelper.COLS];
		hitBoard = new Tile[BoardHelper.ROWS][BoardHelper.COLS];
		
		// Set all tiles to 'EMPTY'
		for (int row = 0; row < BoardHelper.ROWS; row++)
			for (int col = 0; col < BoardHelper.COLS; col++)
			{
				playerBoard[row][col] = new Tile(row, col, false);
				hitBoard[row][col] = new Tile(row, col, true);
			}
		
		// Test board
		playerBoard[3][3].changeType(TileType.AC_START);
		playerBoard[3][4].changeType(TileType.AC_BODY);
		playerBoard[3][5].changeType(TileType.AC_BODY);
		playerBoard[3][6].changeType(TileType.AC_BODY);
		playerBoard[3][7].changeType(TileType.AC_BODY);
	}
	
	
}
